/*
 */
package aptech.eproject.logictics.dao.kien;

import aptech.eproject.logictics.db.Account;
import aptech.eproject.logictics.db.Branch;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Kien Nguyen <solidfoxrock at gmail.com>
 */
@Stateless
public class BranchService implements BranchServiceRemote {

    @PersistenceContext
    private EntityManager em;

    /**
     *
     * @param branchId
     * @return
     */
    @Override
    public Branch findBranch(int branchId) {
        return em.find(Branch.class, branchId);
    }


    /**
     * return Branch of Employee
     * @param userName
     * @return null if userName is a customer, admin or manager
     */
    public Branch findBranchByUserName(String userName) {
        Account account = em.find(Account.class, userName);
        if (account != null) {
            Branch branch = account.getBranch();
            if (branch != null) {
                return branch;
            }
        }
        return null;
    }

    /**
     *
     * @return
     */
    @Override
    public List<Branch> findBranches() {
        Query q = em.createNamedQuery("findBranches");
        return q.getResultList();
    }

    /**
     *
     * @param cityName
     * @return
     */
    @Override
    public List<Branch> findBranchesByCity(String cityName) {
        Query q = em.createNamedQuery("findBranchesByCity");
        q.setParameter(1, "%" + cityName + "%");
        return q.getResultList();
    }

    /**
     *
     * @param address
     * @return
     */
    @Override
    public List<Branch> findBranchesByAddress(String address) {
        Query q = em.createNamedQuery("findBranchesByAddress");
        q.setParameter(2, "%" + address + "%");
        return q.getResultList();
    }

    /**
     *
     * @param branch
     */
    @Override
    public void create(Branch branch) {
        Branch b = em.find(Branch.class, branch.getBranchId());
        if (b != null && b.getIsDeleted()) {
            branch.setIsDeleted(false);
            em.merge(branch.getCity());
            em.merge(branch);
        } else {
            em.persist(branch);
        }
    }

    /**
     *
     * @param branch
     */
    @Override
    public void edit(Branch branch) {
        em.merge(branch.getCity());
        em.merge(branch);
    }

    /**
     *
     * @param branchId
     */
    @Override
    public void remove(int branchId) {
        Branch branch = em.find(Branch.class, branchId);
        if (branch == null) {
            throw new EntityNotFoundException("The branch with id " + branchId + " does not exist");
        }
        branch.setIsDeleted(true);
        em.merge(branch);
    }

    /**
     * 
     * @param branchId
     */
    @Override
    public void recover(int branchId) {
        Branch branch = em.find(Branch.class, branchId);
        if (branch == null) {
            throw new EntityNotFoundException("The branch with id " + branchId + " does not exist");
        }
        branch.setIsDeleted(false);
        em.merge(branch);
    }

    /**
     * for testing only
     * @param branchId
     */
    @Override
    public void removeForever(int branchId) {
        Branch branch = em.find(Branch.class, branchId);
        if (branch != null) {
            em.remove(branch);
        }
    }
}
